/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ereconquista.web;

import com.ereconquista.dao.MonedaDAO;
import com.ereconquista.modelo.Moneda;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

/**
 *
 * @author ctrosch
 */
@ManagedBean(name="monedaBean")
@SessionScoped
public class MonedaBean implements Serializable {
    
    private MonedaDAO monedaDAO;
    private List<Moneda> lista;

    /** Creates a new instance of MonedaBean */
    public MonedaBean(){
    }

    @PostConstruct
    public void init(){

        try {  
            monedaDAO = new MonedaDAO();
            lista = monedaDAO.getLista();
        } catch (Exception e) {
            System.err.println("Error al iniciar "+ getClass().getSimpleName());
            e.printStackTrace();
        }
    }

    public List<Moneda> getLista() {
        return lista;
    }

    public void setLista(List<Moneda> lista) {
        this.lista = lista;
    }
    
    public Converter getConverterMoneda() {

        return new Converter() {

            @Override
            public Object getAsObject(FacesContext context, UIComponent component, String value) {
                if (value.trim().equals("") || value == null) {
                    return null;
                }                
                Moneda m = monedaDAO.getMoneda(value);                
                return m;
            }

            @Override
            public String getAsString(FacesContext context, UIComponent component, Object value) {
                if (value == null || value.equals("")) {
                    return "";
                } else {
                    return ((Moneda) value).getCodigo()+ "";
                }
            }
        };
    }
    
}
